/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserInfo.java
 *
 * Created on Dec 5, 2011, 2:45:41 PM
 */
package anbserver;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author An
 */
public class UserInfo extends javax.swing.JFrame {

    /** Creates new form UserInfo */
    public UserInfo() {
        dcbm = new DefaultComboBoxModel();
        dcbm.addElement(1);
        dcbm.addElement(3);
        dcbm.addElement(5);
        dcbm.addElement(7);

        initComponents();
    }
    private DefaultComboBoxModel dcbm;
    Timer timer;

    void actTimer(int timeDelay) {
        timer = new Timer(timeDelay * 1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                showAccount();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlButton = new javax.swing.JPanel();
        btnRefresh = new javax.swing.JButton();
        ckbAuto = new javax.swing.JCheckBox();
        cbbSeconds = new javax.swing.JComboBox();
        lbAuto = new javax.swing.JLabel();
        lbRefresh = new javax.swing.JLabel();
        lbSeconds = new javax.swing.JLabel();
        pnlAccount = new javax.swing.JPanel();
        scrAccount = new javax.swing.JScrollPane();
        tbAccount = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("User Account");

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        ckbAuto.setText("ckbAuto");
        ckbAuto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ckbAutoActionPerformed(evt);
            }
        });

        cbbSeconds.setModel(dcbm);
        cbbSeconds.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbSecondsActionPerformed(evt);
            }
        });

        lbAuto.setText("Auto refresh");

        lbRefresh.setText("Refresh per");

        lbSeconds.setText("Seconds");

        javax.swing.GroupLayout pnlButtonLayout = new javax.swing.GroupLayout(pnlButton);
        pnlButton.setLayout(pnlButtonLayout);
        pnlButtonLayout.setHorizontalGroup(
            pnlButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlButtonLayout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addComponent(btnRefresh)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                .addGroup(pnlButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbAuto)
                    .addComponent(lbRefresh))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlButtonLayout.createSequentialGroup()
                        .addComponent(cbbSeconds, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbSeconds))
                    .addComponent(ckbAuto))
                .addGap(31, 31, 31))
        );
        pnlButtonLayout.setVerticalGroup(
            pnlButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlButtonLayout.createSequentialGroup()
                .addContainerGap(28, Short.MAX_VALUE)
                .addGroup(pnlButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbAuto)
                    .addComponent(ckbAuto, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbSeconds, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbSeconds)
                    .addComponent(lbRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27))
            .addGroup(pnlButtonLayout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        getContentPane().add(pnlButton, java.awt.BorderLayout.PAGE_END);

        tbAccount.setModel(model);
        scrAccount.setViewportView(tbAccount);

        javax.swing.GroupLayout pnlAccountLayout = new javax.swing.GroupLayout(pnlAccount);
        pnlAccount.setLayout(pnlAccountLayout);
        pnlAccountLayout.setHorizontalGroup(
            pnlAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrAccount, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        pnlAccountLayout.setVerticalGroup(
            pnlAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrAccount, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
        );

        getContentPane().add(pnlAccount, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
    showAccount();
}//GEN-LAST:event_btnRefreshActionPerformed

private void cbbSecondsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbSecondsActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_cbbSecondsActionPerformed

private void ckbAutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ckbAutoActionPerformed
    if (ckbAuto.isSelected()) {
        actTimer((Integer) dcbm.getSelectedItem());
        timer.start();
    } else {
        timer.stop();
    }
}//GEN-LAST:event_ckbAutoActionPerformed
    private void showAccount() {
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String query = "SELECT accName,pin,balance FROM account";
            rs = st.executeQuery(query);
            rsmd = rs.getMetaData();
            Vector<String> columnName = new Vector<String>();
            Vector<String> columnData;
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                columnName.add(rsmd.getColumnName(i + 1));
            }
            // set table column's Name
            model = new DefaultTableModel();
            int cc = rsmd.getColumnCount();
            for (int i = 0; i < cc; i++) {
                model.addColumn(columnName.get(i));
            }

            while (rs.next()) {
                columnData = new Vector<String>();
                for (int i = 0; i < cc; i++) {
                    columnData.add(rs.getString(i + 1));
                    //     System.out.println(rs.getString(i + 1));
                }
                model.addRow(columnData);
                //         columnData.clear();
            }
            tbAccount.setModel(model);
            scrAccount.setViewportView(tbAccount);
            con.close();


        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UserInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UserInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UserInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UserInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new UserInfo().setVisible(true);
//            }
//        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRefresh;
    private javax.swing.JComboBox cbbSeconds;
    private javax.swing.JCheckBox ckbAuto;
    private javax.swing.JLabel lbAuto;
    private javax.swing.JLabel lbRefresh;
    private javax.swing.JLabel lbSeconds;
    private javax.swing.JPanel pnlAccount;
    private javax.swing.JPanel pnlButton;
    private javax.swing.JScrollPane scrAccount;
    private javax.swing.JTable tbAccount;
    // End of variables declaration//GEN-END:variables
    private ConnectDB cdb = null;
    private Connection con;
    private Statement st;
    private ResultSet rs;
    private ResultSetMetaData rsmd;
    private DefaultTableModel model = new DefaultTableModel();
}
